﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Timers;
using OpenCost.Common;
using OpenCost.Common.Attributes;
using OpenCost.Common.Interfaces.Output;
using OpenCost.Common.Readings;


namespace OpenCost.OutputPlugin.GooglePowerMeter
{
    [PlugIn("This plug-in will output to Google Power Meter.")]
    public class GooglePowerMeterWriter : IOutput
    {
        private List<RealTimeReading> realTimeReadings;
        private DateTime lastCommunicationToGoogle;
        private Timer timer;

        public GooglePowerMeterWriter()
        {
            realTimeReadings = new List<RealTimeReading>();
            lastCommunicationToGoogle = DateTime.Now;
            //timer = new Timer(TimeSpan.FromMinutes(15).TotalMilliseconds);
            timer = new Timer(TimeSpan.FromSeconds(15).TotalMilliseconds);
            timer.Elapsed += TimerElapsed;
        }

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            //send to google
            //construct the xml message
            realTimeReadings.Sort(RealTimeReading.CompareByTime);
            //            http://florent.clairambault.fr/google-powermeter-and-sample-c-net-api-usage


        }

        public void SaveReading(Reading newReading)
        {
            switch (newReading.ReadingType)
            {
                case Enums.ReadingTypes.CurrentCostRealTime:
                    realTimeReadings.Add((RealTimeReading)newReading);
                    break;

            }
            Console.Beep(600, 1000);
        }

        public string PluginTitle
        {
            get { return "Google Power Meter Plugin"; }
        }

        private bool _isActive;
        public bool isActive
        {
            get
            {
                return _isActive;
            }
            set
            {
                _isActive = value; timer.Enabled = _isActive;
            }
        }

        public void Show()
        {
            Config config = new Config();
            config.ShowDialog();
        }

        public string FormTitle
        {
            get { return "Google Power Meter Config"; }
        }
    }
}
